package com.hooya.fa.eu.biz.dao.manager.special;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.hooya.fa.eu.api.bo.report.CostwayStkInfoBo;
import com.hooya.fa.eu.api.bo.report.CycleNumberBO;
import com.hooya.fa.eu.api.util.FileUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

import static com.hooya.fa.eu.api.util.DateExtUtil.getYesterday;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/1/10
 * @Version 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class StockListManager {

    public void dealWithStkData(String startDate, List<CostwayStkInfoBo> stkList,
                                Map<String, List<CycleNumberBO>> cycleNumberMap) {
        String start = getYesterday(startDate);
        stkList.forEach(s -> {
            String startKey = s.getSku() + s.getWhId() + s.getBelongs() + start;
            String endKey = s.getSku() + s.getWhId() + s.getBelongs() + s.getDate();
            List<CycleNumberBO> startNumber = cycleNumberMap.get(startKey);
            List<CycleNumberBO> endNumber = cycleNumberMap.get(endKey);
            //处理开始结束的产品数量
            if (CollectionUtils.isEmpty(startNumber)) {
                s.setBeginQty(0);
            } else {
                s.setBeginQty(startNumber.get(0).getBeginQty());
            }
            if (CollectionUtils.isEmpty(endNumber)) {
                s.setEndQty(0);
            } else {
                s.setEndQty(endNumber.get(0).getEndQty());
            }
            s.setUnitPricePln(s.getUnitPrice());
            //处理价格,数量与总价
            if ("EUR".equals(s.getCurrency())) {
                s.setUnitPriceEur(s.getPrice());
                if ("in".equals(s.getDirection())) {
                    s.setEurPriceIn(s.getPrice());
                }
                if ("out".equals(s.getDirection())) {
                    s.setEurPriceOut(s.getUnitPriceEur());
                }
            }
            if ("USD".equals(s.getCurrency())) {
                if ("in".equals(s.getDirection())) {
                    s.setUsdPriceIn(s.getPrice());
                }
                if ("out".equals(s.getDirection())) {
                    s.setUsdPriceOut(s.getPrice());
                }
            }
            if ("PLN".equals(s.getCurrency())) {
                if ("in".equals(s.getDirection())) {
                    s.setPlnPriceIn(s.getPrice());
                }
                if ("out".equals(s.getDirection())) {
                    s.setPlnPriceOut(s.getPrice());
                }
            }
            if (s.getDirection().equals("in")) {
                s.setInboundQty(s.getQty());
                s.setInCostNet(s.getTotalCost());
            }
            if (s.getDirection().equals("out")) {
                s.setOutboundQty(s.getQty());
                s.setOutCostNet(s.getTotalCost());
            }
            //处理发票日期、发票号、数量
            if ("报损".equals(s.getType())) {
                s.setBsOrThQty(s.getQty());
                s.setInvoiceDate(null);
            }
            if ("退货".equals(s.getType())) {
                s.setInvoiceDate(null);
                s.setBsOrThQty(s.getQty());
            }
            if ("转运".equals(s.getType())) {
                s.setZyQty(s.getQty());
                s.setInvoiceDate(null);
                s.setZyInvoiceNo(s.getInvoiceNo());
                s.setInvoiceNo(null);
            }
            if ("B2C".equals(s.getType())) {
                s.setB2cQty(s.getQty());
            }
            if ("B2B".equals(s.getType())) {
                s.setB2bQty(s.getQty());
            }
        });
    }

    public void generateExport(Map<String, List<CostwayStkInfoBo>> belongsStkMap, String date) {
        String year = date.split("-")[0];
        String mon = date.split("-")[1];
        belongsStkMap.forEach((belongs, l) -> {
            File file = FileUtil.createNewFile("stk/", belongs + "-" + year + ".xlsx");
            ExcelWriter excelWriter = null;
            try {
                excelWriter = EasyExcel.write(file.getAbsolutePath()).build();
                WriteSheet writeSheet = EasyExcel.writerSheet(mon).head(CostwayStkInfoBo.class).build();
                excelWriter.write(l, writeSheet);
            } finally {
                if (excelWriter != null) {
                    excelWriter.finish();
                }
            }
        });
    }

}
